package com.hxut.servlet;

import com.hxut.dao.GoodsDao;
import com.hxut.dao.ShoppingCart;
import com.hxut.model.Goods;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "UpdateCartNumberServlet", urlPatterns = "/updateCartNumberServlet")
public class UpdateCartNumberServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int number = Integer.parseInt(req.getParameter("number"));
        int id = Integer.parseInt(req.getParameter("id"));

        GoodsDao goodsDao = new GoodsDao();
        Goods goods = new Goods();
        goods = goodsDao.findGoodsById(id);

        HttpSession session = req.getSession();
        ShoppingCart cart = (ShoppingCart) session.getAttribute("cart");

        cart.updateProductNumber(goods, number);
        session.setAttribute("cart", cart);

        resp.sendRedirect(req.getContextPath() + "/good-cart.jsp");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
